<?php
use think\facade\Db;

    //公共函数
    function sendToken($user_id,$mobile)
    {
        $data = [
            'iss'=>request()->domain(),
            'itm'=>time(),
            'data'=>[
                'user_id'=>$user_id,
                'mobile'=>$mobile
            ]
        ];
        $key = config('app.jwt_key');
        $token =  \Firebase\JWT\JWT::encode($data,$key,'HS256');
        return $token;
    }

    function sendAliSms($mobile)
    {
        $code = mt_rand(1111,9999);
        $config = new \Darabonba\OpenApi\Models\Config([
            // 从阿里云获取
            'accessKeyId' => config('app.accessKeyId'),
            'accessKeySecret' => config('app.accessKeySecret'),
        ]);

        $config->endpoint = "dysmsapi.aliyuncs.com";
        $client = new \AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi($config);

        // 运行时参数超时设置，仅对使用了该运行时参数实例的请求有效
        $runtimeOptions = new \AlibabaCloud\Tea\Utils\Utils\RuntimeOptions();
        // true 忽略证书校验
        $runtimeOptions->ignoreSSL = true;

        $sendSmsRequest = new \AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest();
        // 手机号
        $sendSmsRequest->phoneNumbers = $mobile;
        // 签名名称
        $sendSmsRequest->signName = '阿里云短信测试';
        // 模板CODE
        $sendSmsRequest->templateCode = 'SMS_154950909';
        // 生成随机数作为验证码
        $sendSmsRequest->templateParam = json_encode(['code' => $code]);
        try {
            $response = $client->sendSmsWithOptions($sendSmsRequest, $runtimeOptions);
        } catch (\Exception $exception) {
            halt($exception->getMessage());
        }
        if ($response->body->code == 'OK') {
            Db::name('sms_code')->insert([
                'mobile'=>$mobile,
                'code'=>$code,
                'create_time'=>time()
            ]);
            echo '短信发送成功！';
        }else{
            halt($response);
        }
    }


    //检查验证码
    function CheckSmsCode($mobile,$code)
    {
        $log =\api\user\model\SmsCodeModel::where('mobile','=',$mobile)->find();
        if(!$log)
        {
            return false;
        }
        if($log['code'] != $code)
        {
            return false;
        }
        if(time() - $log['create_time'] > 60)
        {
            \api\user\model\SmsCodeModel::where('mobile','=',$mobile)
                ->delete();
            return false;
        }
            \api\user\model\SmsCodeModel::where('mobile','=',$mobile)
            ->delete();
        return true;
    }
    //支付
    function money_log($user_id,$wallet,$before,$after,$type)
    {
        $money_log = [
            'user_id' => $user_id,
            'wallet' => $wallet,
            'before' => $before,
            'after' => $after,
            'type' => $type,
            'create_time' => date('Y:m:d H:i:s')
        ];
        $res = \api\user\model\MoneyModel::create($money_log);
        if ($res) {
            return json(['code' => '1', 'msg' => '流水记录表添加成功']);
        } else {
            return json(['code' => '0', 'msg' => '流水记录表添加失败']);
        }
    }